import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbattendance } from '../../../../atoms/db';


const logger = anylogger('pages/api/zllweapp/attendancequery/s002.api');



export type Result = {
	ok: true;
	coursename: Pick<ITbattendance, 'coursename'>[]

} | {
	ok: false;
	message: string;
};

export type Message = {
	selected: number;
}

/**
 * pages/api/zllweapp/attendancequery/s002.api
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);

		const { selected } = req.body as Message;


		if (!selected) {
			res.status(200).json({ ok: false, message: '请选择星期' });
			return;
		}
		const student_id = '201942307789';
		const coursename = await tbattendance().distinct('coursename').where({
			week: selected
		}).andWhere({ student_id: student_id });

		res.status(200).json({ ok: true, coursename });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
